package org.opens.lambda;

import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.Test;

import java.util.List;

/**
 * 功能:
 *      1. 使用lambda替换策略模式.
 *      2. 函数式接口@FunctionInterface的说明.
 */
public class UseLambdaReplacementStrategyMode {


    /**
     * 输出:
     *      true
     * 说明:
     *      1. 只需要使自己的接口继承了Predicate接口, 就可以将自己的lambda作为返回值返回(经过强制转换).   #一般不会这么用
     */
    @Test
    public void test4() {
        Filter2 filter = apple -> {
            return "red".equals(apple.getColor());
        };
        System.out.println(filter instanceof Predicate);
    }

    /**
     * 功能:
     *      使用lambda来替换掉策略模式.
     * 输出:
     *      [Apple{name='xiaoA', color='red'}, Apple{name='xiaoB', color='red'}]
     *      [Apple{name='xiaoC', color='green'}]
     * 说明:
     *      1. 可以发现, 一个lambda表达式就相当于一个接口实现;
     *      2.
     */
    @Test
    public void test2() {
        List<Apple> list = Lists.newArrayList();
        list.add(new Apple("xiaoA", "red"));
        list.add(new Apple("xiaoB", "red"));
        list.add(new Apple("xiaoC", "green"));
        List<Apple> res = filterColor(list, apple -> {
            return "red".equals(apple.getColor());
        });
        List<Apple> res1 = filterColor(list, apple -> {
            return "green".equals(apple.getColor());
        });
        System.out.println(res);
        System.out.println(res1);
    }

    @Test
    public void test1() {
        List<Apple> list = Lists.newArrayList();
        list.add(new Apple("xiaoA", "red"));
        list.add(new Apple("xiaoB", "red"));
        list.add(new Apple("xiaoC", "green"));
        List<Apple> res = filterColor(list, FilterFactory.getInstance("red"));
        System.out.println(res);
    }

    public static List<Apple> filterColor(List<Apple> list, Filter filter) {
        List<Apple> res = Lists.newArrayList();
        for(Apple apple : list) {
            if(filter.apply(apple)) {
                res.add(apple);
            }
        }
        return res;
    }

    public static class FilterFactory {
        /**
         * 说明:
         *      1. 可以发现, 函数式编程可以返回函数, 这样就可以直接以一个工厂返回根据参数的不同而决定返回值的接口实现.
         *          - Filter接口很重要, 这个是lambda进行类型推断的核心.
         *          - 所以现在, 我们可以根据
         * @param color
         * @return
         */
        public static Filter getInstance(String color) {
            return apple -> {
                if(color == null) {
                    return false;
                }
                return color.equals(apple.getColor());
            };
        }

        public static Filter2 getInstance1(String color) {
            Predicate<Apple> predicate = apple -> {
                if (color == null || apple == null) {
                    return false;
                }
                return "green".equals(apple.getColor());
            };
            if("red".equals(color)) {
                predicate = apple -> {
                    if (color == null || apple == null) {
                        return false;
                    }
                    return "green".equals(apple.getColor());
                };
            }
            return (Filter2) predicate;
        }

        public static Filter getInstance2(String color) {
            Filter filter = new FilterOne();
            if("red".equals(color)) {
                filter = new FilterTwo();
            }
            return filter;
        }

    }

    public static class FilterOne implements Filter {
        @Override
        public boolean apply(Apple apple) {
            return "green".equals(apple.getColor());
        }
    }

    public static class FilterTwo implements Filter {
        @Override
        public boolean apply(Apple apple) {
            return "red".equals(apple.getColor());
        }
    }

    @FunctionalInterface
    public static interface Filter {
        public boolean apply(Apple apple);
        //public boolean apply1(Apple apple);函数式接口只能有一个public抽象方法, 不能有第二个, 否则该注解会报错(并不是接口报错, 而是注解报错)!
        //可以有默认方法
        default String getColor(Apple apple) {
            return apple.getColor();
        }

    }

    @FunctionalInterface
    public static interface Filter2 extends Predicate<Apple> {

        @Override
        public boolean apply(Apple apple);

        //public boolean apply1(Apple apple);函数式接口只能有一个public抽象方法, 不能有第二个, 否则该注解会报错(并不是接口报错, 而是注解报错)!

        //可以有默认方法
        default String getColor(Apple apple) {
            return apple.getColor();
        }

    }

    public static class Apple {

        private String name;

        private String color;

        public Apple() {
        }

        public Apple(String name, String color) {
            this.name = name;
            this.color = color;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        @Override
        public String toString() {
            return "Apple{" +
                    "name='" + name + '\'' +
                    ", color='" + color + '\'' +
                    '}';
        }

    }

}
